package com.xuecheng.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.mapper.XcChooseCourseMapper;
import com.xuecheng.learning.mapper.XcCourseTablesMapper;
import com.xuecheng.learning.model.dto.XcChooseCourseDto;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.model.po.XcChooseCourse;
import com.xuecheng.learning.model.po.XcCourseTables;
import com.xuecheng.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Linzkr
 * @description: TODO
 * @date 2023/2/2 9:58
 */
@Service
@Slf4j
public class MyCourseTablesServiceImpl implements MyCourseTablesService {
    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    XcCourseTablesMapper xcCourseTablesMapper;
    @Autowired
    XcChooseCourseMapper xcChooseCourseMapper;

    @Override
    public XcChooseCourseDto addCourseCourse(String userId, Long courseId) {
        CoursePublish coursePublish = contentServiceClient.getCoursePublish(courseId);
        String charge = coursePublish.getCharge();
//        如果收费规则是免费的直接添加免费课程到选课记录表和我的课程表
        XcChooseCourse xcChooseCourse=new XcChooseCourse();
        if ("201000".equals(charge)) {
             xcChooseCourse = addFreeCourse(userId, coursePublish);

        } else {
//            如果是收费课程那么先只添加到选课记录表
             xcChooseCourse = addChargeCourse(userId, coursePublish);

        }
//        获取用户对该课程的学习资格
        XcCourseTablesDto xcCourseTablesDto = getLearningStatus( userId, courseId);
//        封装结果对象
        XcChooseCourseDto xcChooseCourseDto =new XcChooseCourseDto();
        BeanUtils.copyProperties(xcChooseCourse,xcChooseCourseDto );
        xcChooseCourseDto.setLearnStatus(xcCourseTablesDto.getLearnStatus());
        return xcChooseCourseDto;
    }
    /**
     * @Author Linzkr
     * @Description 判断用户对该课程是可以正常学习还是没有选课还是已经过期
     * @Date 2023/2/2 11:34
     * @param userId
     * @param courseId
     * @return XcCourseTablesDto 学习资格状态 [{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
     */
    @Override
    public XcCourseTablesDto getLearningStatus(String userId, Long courseId) {
        XcCourseTables xcCourseTables = getXcCourseTables(userId, courseId);
        XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
        if (xcCourseTables==null){
//            如果查询不到课程 代表用户没有选课或者未支付订单 设置702002
            xcCourseTablesDto.setLearnStatus("702002");
            return xcCourseTablesDto;
        }
        BeanUtils.copyProperties(xcCourseTables, xcCourseTablesDto);
//        如果当前事件超过了选课的有效时间那么就设置702003
        if (LocalDateTime.now().isAfter(xcCourseTables.getValidtimeEnd())){
            xcCourseTablesDto.setLearnStatus("702003");
            return xcCourseTablesDto;
        }
//        正常学习
        xcCourseTablesDto.setLearnStatus("702001");
        return xcCourseTablesDto;
    }

    @Transactional
    //添加免费课程,免费课程加入选课记录表、我的课程表
    public XcChooseCourse addFreeCourse(String userId, CoursePublish coursePublish) {
        Long courseId = coursePublish.getId();
//        添加免费课程需要先查找选课记录表中是否有记录 如果有记录 直接返回  如果没有记录 添加记录
        LambdaQueryWrapper<XcChooseCourse> xcChooseCourseLambdaQueryWrapper = new LambdaQueryWrapper<XcChooseCourse>()
                .eq(XcChooseCourse::getUserId, userId)
                .eq(XcChooseCourse::getCourseId, courseId)
                .eq(XcChooseCourse::getOrderType, "700001")
                .eq(XcChooseCourse::getStatus, "701001");
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(xcChooseCourseLambdaQueryWrapper);
        if (xcChooseCourses != null && xcChooseCourses.size() > 0) {
            return xcChooseCourses.get(0);
        }
//        向选课表添加记录
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(courseId);
        xcChooseCourse.setCourseName(coursePublish.getName());
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setCompanyId(coursePublish.getCompanyId());
        xcChooseCourse.setOrderType("700001");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setCoursePrice(0F);
        xcChooseCourse.setValidDays(coursePublish.getValidDays());
        xcChooseCourse.setStatus("701001");
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(coursePublish.getValidDays()));
        xcChooseCourse.setRemarks(coursePublish.getRemark());
        xcChooseCourseMapper.insert(xcChooseCourse);
//        向我的课程表添加记录
        XcCourseTables xcCourseTables = addCourseTables(xcChooseCourse);
        return xcChooseCourse;

    }

    @Transactional
    //添加收费课程
    public XcChooseCourse addChargeCourse(String userId, CoursePublish coursePublish) {
        Long courseId = coursePublish.getId();
//        添加免费课程需要先查找选课记录表中是否有记录 如果有记录 直接返回  如果没有记录 添加记录
        LambdaQueryWrapper<XcChooseCourse> xcChooseCourseLambdaQueryWrapper = new LambdaQueryWrapper<XcChooseCourse>()
                .eq(XcChooseCourse::getUserId, userId)
                .eq(XcChooseCourse::getCourseId, courseId)
                .eq(XcChooseCourse::getOrderType, "700002")
                .eq(XcChooseCourse::getStatus, "701002  ");
        List<XcChooseCourse> xcChooseCourses = xcChooseCourseMapper.selectList(xcChooseCourseLambdaQueryWrapper);
        if (xcChooseCourses != null && xcChooseCourses.size() > 0) {
            return xcChooseCourses.get(0);
        }
//        向选课表添加记录
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(courseId);
        xcChooseCourse.setCourseName(coursePublish.getName());
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setCompanyId(coursePublish.getCompanyId());
        xcChooseCourse.setOrderType("700002");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setCoursePrice(coursePublish.getPrice());
        xcChooseCourse.setValidDays(coursePublish.getValidDays());
        xcChooseCourse.setStatus("701002");
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(coursePublish.getValidDays()));
        xcChooseCourse.setRemarks(coursePublish.getRemark());
        xcChooseCourseMapper.insert(xcChooseCourse);

        return xcChooseCourse;
    }

    @Transactional
    public XcCourseTables addCourseTables(XcChooseCourse xcChooseCourse) {
        Long courseId = xcChooseCourse.getCourseId();
        String userId = xcChooseCourse.getUserId();
        XcCourseTables xcCourseTables = getXcCourseTables(userId, courseId);
        if (xcCourseTables!=null){
            return xcCourseTables;
        }
        xcCourseTables = new XcCourseTables();
        Long xcChooseCourseId = xcChooseCourse.getId();
        xcCourseTables.setChooseCourseId(xcChooseCourseId);
        xcCourseTables.setUserId(userId);
        xcCourseTables.setCourseId(courseId);
        xcCourseTables.setCompanyId(xcChooseCourse.getCompanyId());
        xcCourseTables.setCourseName(xcChooseCourse.getCourseName());
        xcCourseTables.setCourseType(xcChooseCourse.getOrderType());
        xcCourseTables.setCreateDate(xcChooseCourse.getCreateDate());
        xcCourseTables.setValidtimeStart(xcChooseCourse.getValidtimeStart());
        xcCourseTables.setValidtimeEnd(xcChooseCourse.getValidtimeEnd());
        //添加到我的课程表
        xcCourseTablesMapper.insert(xcCourseTables);

        return xcCourseTables;




    }

    public XcCourseTables getXcCourseTables(String userId,Long courseId){
        XcCourseTables xcCourseTables = xcCourseTablesMapper.selectOne(new LambdaQueryWrapper<XcCourseTables>()
                                        .eq(XcCourseTables::getUserId, userId)
                                        .eq(XcCourseTables::getCourseId, courseId));
        return xcCourseTables;

    }

    @Override
    public boolean saveChooseCourseStauts(String choosecourseId) {
        return false;
    }
}
